﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
namespace DAO
{
    public class DonHangDAO
    {
        public static DonHangDTO LayDonHangTuDataRow(DataRow dr)
        {
            DonHangDTO dh = null;
            try
            {
                dh = new DonHangDTO();
                dh.MaDonHang = dr["MaDonHang"].ToString();
                dh.MaTinBan = dr["MaTinBan"].ToString();
                dh.MaTaiKhoanMua = dr["MaTaiKhoanMua"].ToString();
                dh.DonGia = int.Parse(dr["DonGia"].ToString());
                dh.SoLuongMua = int.Parse(dr["SoLuongMua"].ToString());
                dh.ThanhTien = int.Parse(dr["ThanhTien"].ToString());
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi lấy thông tin đơn hàng: " + ex.ToString());
            }
            return dh;
        }

        public static IList<SqlParameter> TruyenThamSoChoStored(DonHangDTO dh)
        {
            IList<SqlParameter> arrParameter = new List<SqlParameter>();
            arrParameter.Add(new SqlParameter("@MaDonHang", dh.MaDonHang));
            arrParameter.Add(new SqlParameter("@TenTaiKhoan", dh.MaTinBan));
            arrParameter.Add(new SqlParameter("@MatKhau", dh.MaTaiKhoanMua));
            arrParameter.Add(new SqlParameter("@CMND", dh.DonGia));
            arrParameter.Add(new SqlParameter("@HoTen", dh.SoLuongMua));
            arrParameter.Add(new SqlParameter("@NgaySinh", dh.ThanhTien));
            return arrParameter;
        }

        public static bool MuaNgaySanPham(DonHangDTO dh)
        {
            try
            {
                IList<SqlParameter> arrParameter = new List<SqlParameter>();
                arrParameter.Add(new SqlParameter("@MaDonHang", dh.MaDonHang));
                arrParameter.Add(new SqlParameter("@MaTinBan", dh.MaTinBan));
                arrParameter.Add(new SqlParameter("@MaTaiKhoanMua", dh.MaTaiKhoanMua));
                arrParameter.Add(new SqlParameter("@DonGia", dh.DonGia));
                arrParameter.Add(new SqlParameter("@SoLuongMua", dh.SoLuongMua));
                arrParameter.Add(new SqlParameter("@ThanhTien", dh.ThanhTien));
                DataTable dt = DataProvider.ExecuteStoreProc("MuaNgaySanPham", arrParameter);
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi thêm mới khách hàng " + ex.ToString());
            }
            return false;
        }
    }
}
